//
//  DHFilterViewController.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/5/5.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHFilterViewController.h"
#import "DHProjectCell.h"
#import "DHGoodTypeCell.h"
#import "DHPriceSectionCell.h"
#import "DHStoreTypeCell.h"
NSString *priceSecCellID = @"priceSecCellID";
NSString *projectCellID = @"projectCellID";
NSString *goodTypeCellID = @"goodTypeCellID";
NSString *storeTypeCellID = @"storeTypeCellID";
@interface DHFilterViewController () <UITableViewDataSource, UITableViewDelegate>
@property(nonatomic ,strong)UITableView *filterView;
@end

@implementation DHFilterViewController

- (void)setPlanInfo:(DHPlanInfo *)planInfo{
    _planInfo = planInfo;
    
    //[self.filterView reloadData];
}

- (void)setPlanList:(NSArray *)planList{
    _planList = planList;
    
   // [self.filterView reloadData];

}

#pragma mark: - 懒加载filterView
- (UITableView *)filterView{
    if (!_filterView) {
        _filterView = [[UITableView alloc] init];
        //_filterView.backgroundColor = UIColor.redColor;
        _filterView.dataSource = self;
        _filterView.delegate = self;
    }
    return _filterView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.filterView registerClass:[DHGoodTypeCell class] forCellReuseIdentifier:goodTypeCellID];
    [self.filterView registerClass:[DHProjectCell class] forCellReuseIdentifier:projectCellID];
    [self.filterView registerClass:[DHPriceSectionCell class] forCellReuseIdentifier:priceSecCellID];
    [self.filterView registerClass:[DHStoreTypeCell class] forCellReuseIdentifier:storeTypeCellID];
    
  
    [NSNotificationCenter.defaultCenter addObserverForName:@"kChangeProjectNotifcationName" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        [self.filterView reloadData];
    }];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [self.view layoutIfNeeded];
    self.filterView.frame = CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height);
    [self.view addSubview:self.filterView];
    [self setupNav];
}

- (void)setupNav{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(dismissVc)];
    self.title = @"商品筛选";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"重置" style:UIBarButtonItemStylePlain target:self action:@selector(resetButtonClick)];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
}

#pragma mark: - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getReuseIDWithIdx:indexPath.section]
                                                                                        forIndexPath:indexPath];
    //不复用cell
    while ([cell.contentView.subviews lastObject] != nil) {
        [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell layoutIfNeeded];
    cell.tag = indexPath.section;
    switch (indexPath.section) {
        case 1:
            [(DHProjectCell *)cell setPlanList:self.planList];
            break;
        case 2:
            [(DHProjectCell *)cell setPlanInfo:self.planInfo];
            break;
        default:
            break;
    }
    return cell;
}
#define OneCellHeight 50
#pragma mark: - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 1:
        {
            CGFloat numOfRow = (self.planList.count % 3 == 0) ? self.planList.count / 3 : self.planList.count / 3 + 1;
            return numOfRow * OneCellHeight ;
        }
            break;
        case 2:
        {
            CGFloat numOfRow = (self.planInfo.class_list.count % 3 == 0) ? self.planInfo.class_list.count / 3 :
                                                                            self.planInfo.class_list.count / 3 + 1;
            return numOfRow * OneCellHeight;
        }

            break;
        default:
            return 54;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self creatHeaderViewWitnSection:section];
}
- (UIView *)creatHeaderViewWitnSection:(NSInteger)section{
    
    switch (section) {
        case 0:
        {
            return [self creatSingleHeaderLabelWithText:@"价格区间"];
        }
            break;
        case 1:
        {
            return [self creatSingleHeaderLabelWithText:@"当前方案"];
        }
            break;
        case 2:
        {
            return [self creatSingleHeaderLabelWithText:@"商品分类"];
        }
            break;
        case 3:
        {
            return [self creatSingleHeaderLabelWithText:@"商品类型"];
        }
            break;
        case 4:
        {
            return [self creatSingleHeaderLabelWithText:@"店铺类型"];
        }
            break;
        default:
            return [[UIView alloc] init];
            break;
            
    }
}

- (UILabel *)creatSingleHeaderLabelWithText:(NSString *)text{
    UILabel *label = [[UILabel alloc] init];
    label.text = @"店铺类型";
    label.frame = CGRectMake(8, 0, 40, 20);
    label.font = [UIFont systemFontOfSize:12];
    return label;
}

/**
 返回cellid

 @param idx 索引
 @return NSString
 */
- (NSString *)getReuseIDWithIdx:(NSInteger)idx{
    switch (idx) {
        case 0:
          //  NSLog(@"%@",priceSecCellID);
            return priceSecCellID;
            break;
        case 1:
        case 2:
          //  NSLog(@"%@",projectCellID);

            return projectCellID;
            break;
        case 3:
           // NSLog(@"%@",goodTypeCellID);
            return goodTypeCellID;
        case 4:
            return storeTypeCellID;
        default:
            return @"";
    }
}

// FIXME: 重置按钮监听未实现
- (void)resetButtonClick{
    
}
- (void)dismissVc{
    [self dismissViewControllerAnimated:true completion:nil];
}
@end
